package com.shengde.jinwawaforleader.views;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Rect;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.shengde.jinwawaforleader.HomeApplication;
import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.network.BaseProtocol;

/**
 * Created by Chason on 2016/10/24.
 */

public class ChangeClassNameDialog extends Dialog {

    private Button mPositiveBut;//确定
    private Button mNegativeBut;//取消
    private TextView dailog_title;
    private static Context context;
    private static EditText et_classname;
    private static String classname;

    public ChangeClassNameDialog(Context context) {
        super(context, R.style.UpdateDialog);
        this.context = context;
        setCustomView();
    }

    public ChangeClassNameDialog(Context context, boolean cancelable,
                            OnCancelListener cancelListener) {
        super(context, R.style.UpdateDialog);
        this.setCancelable(cancelable);
        this.setOnCancelListener(cancelListener);
        this.context = context;
        setCustomView();
    }

    public ChangeClassNameDialog(Context context, int theme) {
        super(context, R.style.UpdateDialog);
        this.context = context;
        setCustomView();
    }

    /** 修改确定按钮上的文字 */
    public void setPositiveText(String post) {
        mPositiveBut.setText(post);
    }

    /** 修改取消按钮上的文字 */
    public void setNegativeText(String nega) {
        mNegativeBut.setText(nega);
    }

    public void setDialogTitle(String nega) {
        dailog_title.setText(nega);
    }

    private void setCustomView() {
        View mView = LayoutInflater.from(getContext()).inflate(
                R.layout.change_classname_dialog, null);
        mNegativeBut = (Button) mView.findViewById(R.id.dialog_cancle_but);
        mPositiveBut = (Button) mView.findViewById(R.id.dialog_sure_but);
        dailog_title = (TextView) mView.findViewById(R.id.dailog_title);
        et_classname = (EditText) mView.findViewById(R.id.et_classname);
        et_classname.setText(classname);
        et_classname.setSelection(classname.length());
        mView.setBackgroundResource(R.drawable.bg_d_rect_border);
        super.setContentView(mView);
    }

    @Override
    public void setCanceledOnTouchOutside(boolean cancel) {
        super.setCanceledOnTouchOutside(cancel);
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
    }

    public void setOnPositiveListener(View.OnClickListener listener) {
        mPositiveBut.setOnClickListener(listener);
    }

    public void setOnNegativeListener(View.OnClickListener listener) {
        mNegativeBut.setOnClickListener(listener);
    }

    public static void ShowCustomDialog(final Activity context, final String name , final String classId) {
        classname = name;
        final ChangeClassNameDialog dialog = new ChangeClassNameDialog(context);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setOnPositiveListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(TextUtils.isEmpty(et_classname.getText().toString())) {
                    HomeApplication.showToastShort("班级名称不能为空");
                }else {
                    BaseProtocol.getInstance().changeClassName(context,et_classname.getText().toString(),classId);
                    dialog.dismiss();
                }
            }
        });

        dialog.setOnNegativeListener(new View.OnClickListener() {//看看再说
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        ViewGroup.LayoutParams lay = dialog.getWindow().getAttributes();
        DisplayMetrics dm = new DisplayMetrics();//获取屏幕分辨率
        context.getWindowManager().getDefaultDisplay().getMetrics(dm);//
        Rect rect = new Rect();
        View view = context.getWindow().getDecorView();
        view.getWindowVisibleDisplayFrame(rect);
        lay.width = dm.widthPixels * 9 / 10;
        dialog.show();
    }
}

